/**
 * Created by renzhiqiang on 17/4/25.
 */



import CookieUtils from './cookieUtils'
/**
 * 对当前登录用户的数据进行管理
 */
const clearLoginInfo = ()=>{
  CookieUtils.unset('login_cookie');
  localStorage.removeItem('loginInfo_crm');
}
const setLoginInfo = (data)=>{
  let expires = (new Date())+7;
  CookieUtils.set('login_cookie', '1', expires);//设置登录过期cookie
  let roleList = data.roleList;
  let roles = [];
  for(let i=0; i<roleList.length; i++){
    roles.push(roleList[i]['code']);
  }
  data['roles'] = roles.length >0 ? roles.join() : '';
  localStorage.setItem('loginInfo_crm', JSON.stringify(data));
}
const getUserInfo = (key) => {
  const user = JSON.parse(localStorage.getItem('loginInfo_crm'));
  if (key) {
    return user[key];
  } else {
    return user;
  }
}
const isDataOwner = (createBy) => {
  if (getUserInfo('staffId') === createBy)
    return true;
  else
    return false;
}//是否数据的所有者
const isTheOrgManager = function (OrgId) {
  if (OrgId == getUserInfo('mainBelongOrgId') && OrgId == getUserInfo('mainManageOrgId')) {
    return true;
  }
  return false;
}//用户是公司、组织的leader
const isTheDeptManager = function (deptId) {
  if (deptId == getUserInfo('mainBelongDeptId') && deptId == getUserInfo('mainManageDeptId')) {
    return true;
  }
  return false;
}//用户是部门的leader
const isLeader = function (data) {
  var isLeader = false;
  data = data || {
      ownerDeptId: getUserInfo('mainBelongDeptId'),
      ownerOrgId: getUserInfo('mainBelongOrgId'),
    };

  /* ownerDeptId  //所属部门id
   * ownerOrgId   //所属公司id
   * */
  if (data.ownerOrgId > 0) {
    if (this.isTheOrgManager(data.ownerOrgId)) {
      isLeader = true;
    }
  }
  if (data.ownerDeptId > 0) {
    if (this.isTheDeptManager(data.ownerDeptId)) {
      isLeader = true;
    }
  }
  return isLeader
}//是否是部门领导

module.exports = {
  isLeader,
  getUserInfo,
  isDataOwner,
  setLoginInfo,
  clearLoginInfo,
  isTheOrgManager,
  isTheDeptManager,
}
